package com.abc.robot01.activity

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.abc.robot01.R
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.RetrofitService.Companion.BASE_URL_H5
import com.abc.robot01.retrofit.bean.Condition21
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import com.abc.robot01.rn.MainActivity

import com.abc.robot01.vivemodel.Robot
import kotlinx.coroutines.launch


/**
 * 户型调整
 */
class HuXingTiaoZheng : BaseVoiceActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_h52)
        val housePlansId = intent.getStringExtra("housePlansId")
        val designPackageId = intent.getStringExtra("designPackageId")
        Log.e(TAG, "housePlansId:"+housePlansId)

        lifecycleScope.launch {
//            val  url =   fileUrl+"&id=${housePlansId}&type=1&access_token=${Robot.accessToken}"
            val baseUrl = "${BASE_URL_H5}robot/imbedding/adjust/"
            val url = Uri.Builder()
                .encodedPath(baseUrl)
                .appendQueryParameter("access_token", "${Robot.accessToken}")
                .appendQueryParameter("companyId", "${Robot.companyId}")
                .appendQueryParameter("userId", "${Robot.userId}")
                .appendQueryParameter("type", "1")
                .appendQueryParameter("id", "${housePlansId}")
                .appendQueryParameter("designPackageId", "${designPackageId}")
                .build()
                .toString()

            Log.e(TAG, "fileUrl:"+url)
            val webView = findViewById<WebView>(R.id.webView)

            webView.settings.javaScriptEnabled = true
            webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
            webView.settings.domStorageEnabled = true
            webView.webViewClient = WebViewClient()
            // 设置WebViewClient以在当前应用内打开页面
            webView.loadUrl(url)
            webView.webChromeClient = WebChromeClient()
            // 添加 JavaScriptInterface，用于 JS 调用 Android 方法
            webView.addJavascriptInterface(WebAppInterface(this@HuXingTiaoZheng), "AndroidInterface")
        }



//findViewById<View>(R.id.load).setOnClickListener {
//    webView.loadUrl(url)
//}
//
//        findViewById<View>(R.id.b02).setOnClickListener {
//            // 清除 WebView 缓存，第二个参数 true 表示删除磁盘缓存
//            webView.clearCache(true)
//        }
    }

    // JS 调用的接口
    class WebAppInterface(private val context: HuXingTiaoZheng) {
        // 注解使得这个方法可以被 JavaScript 调用
        @JavascriptInterface
        fun jsToAndroidStart() {
            Log.e(TAG, "jsToAndroidStart:")
            Toast.makeText(context, "jsToAndroidStart", Toast.LENGTH_SHORT).show()
        }

        @JavascriptInterface
        fun back() = context.finish()

        @JavascriptInterface
        fun load(housePlansDataId:String) = context.go(housePlansDataId)
    }

    fun  go(housePlansDataId:String){
         lifecycleScope.launch {
         val s = RetrofitService.instance.getCompanyMachine(accessToken=Robot.accessToken,
                 userId = Robot.userId,
                 machineSn = Robot.sn,
                 companyId = Robot.companyId,
             request = FetchDataRequest9(Condition21(companyId= Robot.companyId))
                 )
             Log.e(TAG, "设备 :"+ s.data.total)
             if(s.data.total!=0){
                 startActivity<MainActivity>{
                     putExtra("housePlansDataId",housePlansDataId)
                     putExtra("name","name is null")
                     putExtra("designPackageId",intent.getStringExtra("designPackageId"))
                     putExtra("housePlansId",intent.getStringExtra("housePlansId"))
                 }
             } else
             {
                 startActivity<RenderActivity>{
                     putExtra("housePlansDataId",housePlansDataId)
                     putExtra("name","name is null")
                     putExtra("taskId","")
                     putExtra("designPackageId",intent.getStringExtra("designPackageId"))
                     putExtra("housePlansId",intent.getStringExtra("housePlansId"))
                 }
             }
         }
        }

    companion object {
        val TAG = "H5"
    }

}
